import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { TranslateService } from '@ngx-translate/core';

import { GlobalData } from '../../providers/GlobalData';

/**
 * Generated class for the SettingsLanguagePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-settings-language',
  templateUrl: 'settings-language.html',
})
export class SettingsLanguagePage {

  constructor(public navCtrl: NavController,
    private global: GlobalData,
    private storage: Storage,
    private viewCtrl: ViewController,
    public translate: TranslateService,
    public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingsLanguagePage');
  }

  selectedDeviceLang(deviceLang) {
    console.log('selectedDeviceLang', deviceLang);
    this.global.curLang = deviceLang;

    this.storage.get('settings').then(setting => {

      setting.locale = deviceLang;
      this.translate.use(this.global.curLang);

      this.storage.set('settings', setting);
    });
  }

  selectedPrintLang(printLang) {
    console.log('selectedPrintLang', printLang);
    this.global.curPrintLang = printLang;

    this.storage.get('settings').then(setting => {

      setting.print_locale = printLang;

      this.storage.set('settings', setting);
    });
  }

  // dismiss table list modal
  dismiss() {
    this.viewCtrl.dismiss();
  }
}
